package utils

import (
	"io"
	"os"
	"path"
	"path/filepath"
)

func SaveFile(fullName string, file io.Reader) error {
	//当前目录
	pwd, err := os.Getwd()
	if err != nil {
		panic("获取当前目录失败:" + err.Error())
	}
	dirFileName := path.Join(pwd, fullName)

	dir := filepath.Dir(dirFileName)
	//如果不存在，就创建目录
	if err := os.MkdirAll(dir, os.ModePerm); err != nil {
		panic("创建目录失败" + err.Error())
	}

	// 创建文件以保存图片
	out, err := os.Create(dirFileName)
	if err != nil {
		panic("Error creating file:" + err.Error())
	}
	defer out.Close()

	_, err = io.Copy(out, file)
	if err != nil {
		panic("Error saving image:" + err.Error())
	}
	return err
}
